package com.zuijin.vue2blog.dto;

import com.zuijin.vue2blog.entity.Role;
import com.zuijin.vue2blog.entity.UserCom;
import com.zuijin.vue2blog.entity.UserRole;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * ClassName:    UserComDetails
 * CreateBy:     IntelliJ IDEA
 * Author:       醉瑾
 * Date:         2022-04-22
 * Description : Security使用的用户详情
 */
public class UserComDetails implements UserDetails {
    // 用户
    private UserCom userCom;

    private List<Role> roles;

    // TODO 增加角色和权限信息
    public UserComDetails(UserCom userCom, List<Role> roles) {
        this.userCom = userCom;
        this.roles = roles;
    }

    /**
     * 获取用户所有角色
     *
     * @return 用户角色
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role role : this.roles) {
            authorities.add(new SimpleGrantedAuthority("ROLE_test" + role.getRoleName()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return userCom.getPassword();
    }

    @Override
    public String getUsername() {
        return userCom.getUsername();
    }

    /**
     * 账户是否过期
     *
     * @return 布尔值
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否锁定
     *
     * @return 布尔值
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 账户是否过期
     *
     * @return 布尔值
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否可用
     *
     * @return 布尔值
     */
    @Override
    public boolean isEnabled() {
        return userCom.getState().equals(0);
    }

    // 自定义的需要的用户信息
    public Integer getUid() {
        return userCom.getId();
    }

    public String getNickname() {
        return userCom.getNickname();
    }
}
